import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
<<<<<<< HEAD
import config from '../config'
const accessToken = sessionStorage.getItem('accessToken'); //不安全。应该使用HTTP Only的Cookie。
=======
//axios
import config from '../config'
>>>>>>> 6e8b3c82a561ae16179a9277d9e4c4a13b5b4a67
const $message = {
    error: (msg: string) => {
        console.error(msg);
    }
<<<<<<< HEAD
};
class Request {
    private baseConfig: AxiosRequestConfig = {
        baseURL: config.domain,
        timeout: 10000,
        headers: {
            'Content-Type': 'application/json', // 发送JSON格式的请求体
            'Accept': 'application/json', // 接受JSON格式的响应体
            // 如果需要认证，可以添加Authorization头部
            'Authorization': `Bearer ${accessToken}`,
            'User-Agent': 'YourApp/1.0',
            'X-Requested-With': 'XMLHttpRequest',
        },
=======
}; class Request {
    private baseConfig: AxiosRequestConfig = {
        baseURL: 'http://localhost:3000/',
        timeout: 5000,
        headers: {},
>>>>>>> 6e8b3c82a561ae16179a9277d9e4c4a13b5b4a67
    }
    private instance: AxiosInstance = axios.create(this.baseConfig)
    constructor() {
        this.setReqInterceptors()
        this.setResnterceptors()
    }
    public setHeader = (headers: any) => {
        this.baseConfig.headers = { ...this.baseConfig.headers, ...headers }
        this.instance = axios.create(this.baseConfig)
        this.setReqInterceptors()
        this.setResnterceptors()
    }
    public get = (
        url: string,
        data: any = {},
        config: AxiosRequestConfig = {},
    ): Promise<any> => {
        return this.instance({
            ...{ url, method: 'get', params: data },
            ...config,
        })
    }
    public post = (
        url: string,
        data: any = {},
        config: AxiosRequestConfig = {},
    ): Promise<any> => {
        return this.instance({
            ...{ url, method: 'POST', data },
            ...config,
        })
    }
    // 不经过统一的axios实例的post请求
    public getOnly = (
        url: string,
        data: any = {},
        config: AxiosRequestConfig = {},
    ) =>
        axios({
            ...this.baseConfig,
            ...{ url, method: 'get', params: data },
            ...config,
        })
    // delete请求,后端通过requestBody接收
    public deleteBody = (
        url: string,
        data: any = {},
        config: AxiosRequestConfig = {},
    ) =>
        this.instance({
            ...{ url, method: 'delete', data },
            ...config,
        })

    // delete请求,后端通过后端通过requestParam接收
    public deleteParam = (
        url: string,
        data: any = {},
        config: AxiosRequestConfig = {},
    ) =>
        this.instance({
            ...{ url, method: 'delete', params: data },
            ...config,
        })
    // put请求,后端通过requestBody接收
    public put = (
        url: string,
        data: any = {},
        config: AxiosRequestConfig = {},
    ): Promise<any> => {
        return this.instance({
            ...{ url, method: 'PUT', data },
            ...config,
        });
    };
    // 请求拦截器
    private setReqInterceptors = () => {
        this.instance.interceptors.request.use(
            (config) => {

                return config
            },
            (err) => {
                $message.error('请求失败')
                return Promise.reject(err)
            },
        )
    }

    // 响应拦截器
    private setResnterceptors = () => {
        this.instance.interceptors.response.use(
            (res) => {
                const { code, data, msg } = res.data
                if (code === 200) {
                    return data
                }
                $message.error(msg || '获取数据失败')
                return Promise.reject(res)
            },
            (err) => {
                $message.error('服务器响应失败')
                return Promise.reject(err)
            },
        )
    }
}

export default new Request()